<?php

    class html_table
    {

        private $columns = 0;
        private $columns_current = 0;
        private $headers = Array();
        private $thead_is_open = false;
        private $head_is_open = false;
        private $row_is_open = false;
        private $tbody_is_open = false;
        private $cell_is_open = false;
        private $max_cols_count = null;
        private $columns_count_initialized = false;

        function __construct($max_cols_count = null, $classname = null, $width = null, $cellspacing = 0, $cellpadding = 0, $headers = null, $id = null)
        {
            $this->max_cols_count = (int)$max_cols_count;
            $parameters = Array();
            $border = 0;
            if(!is_null($id))
            {
                $parameters[] = sprintf('id="%s"', $id);
            }
            if(!is_null($classname))
            {
                $parameters[] = sprintf('class="%s"', $classname);
            }
            if(!is_null($width))
            {
                $parameters[] = sprintf('width="%s"', $width);
            }
            if(!is_null($border))
            {
                $parameters[] = sprintf('border="%s"', $border);
            }
            if(!is_null($cellspacing))
            {
                $parameters[] = sprintf('cellspacing="%s"', $cellspacing);
            }
            if(!is_null($cellpadding))
            {
                $parameters[] = sprintf('cellpadding="%s"', $cellpadding);
            }
            echo sprintf('<table %s>', implode(' ', $parameters),
                $classname, $width, (int)$cellspacing, (int)$cellpadding);
            if(is_array($headers))
            {
                foreach($headers as $header)
                {
                    $this->addhead($header);
                }
            }
        }

        public static function MakeTDParams(
                        $nowrap,
                        $horaligncenter, 
                        $classname,
                        $width, 
                        $verticalalign,
                        $user_script,
                        $colspan)
        {
            $styles = Array();
            $attributes = Array();
            if($nowrap){$styles[] = 'white-space:nowrap';}
            if(!is_null($classname)){$attributes[] = sprintf('class="%s"', $classname);}
            if(!is_null($user_script)){$attributes[] = $user_script;}
            if(!is_null($width)){$attributes[] = sprintf('width="%s"', $width);}
            if(is_numeric($colspan)){$attributes[] = sprintf('colspan="%s"', $colspan);}
            if(!is_null($verticalalign))
            {
                switch($verticalalign)
                {
                    case 'top': case 'middle': case 'bottom': case 'center':
                        $styles[] = 'vertical-align:'.$verticalalign;
                        break;
                }
            }
            if(!is_null($horaligncenter))
            {
                if(is_bool($horaligncenter)){$horaligncenter = $horaligncenter?'center':null;}
                switch($horaligncenter)
                {
                    case 'left': case 'middle': case 'center': case 'right':
                        $styles[] = 'text-align:'.$horaligncenter;
                        break;
                }
            }
            if((count($styles) > 0) && (count($attributes) > 0))
            {
                return ' '.trim(sprintf('style="%s" %s', implode(';', $styles), implode(' ', $attributes)));
            }
            elseif(count($styles) > 0)
            {
                return ' '.sprintf('style="%s"', implode(';', $styles));
            }
            elseif(count($attributes) > 0)
            {
                return ' '.implode(' ', $attributes);
            }
            return null;
        }

        /**
        * Add table head-column with specific caption
        * 
        * @param string $caption
        */
        public function addhead($caption = '&nbsp;')
        {
            if($this->columns > $this->max_cols_count)
            {
                if($this->max_cols_count > 0)
                {
                    return false;
                }
            }
            if(!$this->thead_is_open)
            {
                echo '<thead><tr>';
                $this->thead_is_open = true;
            }
            if($this->head_is_open)
            {
                echo '</th>';
            }
            echo '<th>', $caption;
            $this->head_is_open = true;
            $this->columns++;
            return true;
        }

        private function check_heads_function()
        {
            if(!$this->columns_count_initialized)
            {
                $this->columns_count_initialized = true;
                if($this->max_cols_count > $this->columns)
                {
                    $this->columns = $this->max_cols_count;
                }
            }
            if($this->thead_is_open)
            {
                if($this->head_is_open)
                {
                    echo '</th>';
                    $this->head_is_open = false;
                }
                echo '</tr></thead>';
                $this->thead_is_open = false;
            }
            if(!$this->tbody_is_open)
            {
                $this->tbody_is_open = true;
                echo '<tbody>';
            }
        }

        function newcol($text = null, $nowrap = false, $horaligncenter = false, $rowid = null, $classname = null, $width = null, $verticalalign = null, $user_script = null, $colspan = null, $hiddenrow = false)
        {
            if(!$this->row_is_open)
            {
                $this->newrow($rowid, $hiddenrow);
            }
            $this->check_heads_function();
            $td_parameters = html_table::MakeTDParams($nowrap, $horaligncenter, $classname, $width, $verticalalign, $user_script, $colspan);
            if(!is_numeric($colspan))
            {
                $colspan = 1;
            }
            if($this->cell_is_open)
            {
                $this->closecol_function();
            }
            if($this->columns > 0)
            {
                if($this->columns_current % $this->columns == 0)
                {
                    if($this->columns_current > 0)
                    {
                        $this->newrow();
                    }
                }
            }
            echo '<td'.$td_parameters.'>';
            $this->cell_is_open = true;
            if(!is_null($text))
            {
                echo $text;
            }
            $this->columns_current +=$colspan;
        }

        function newrow($id = null, $hidden = false)
        {
            $this->check_heads_function();
            if($this->row_is_open)
            {
                if($this->cell_is_open)
                {
                    $this->closecol_function();
                }
                for($i = $this->columns_current; $i < $this->columns; $i++)
                {
                    $this->newcol();
                    $this->closecol_function();
                }
                $this->closerow_function();
            }
            elseif($this->cell_is_open)
            {
                $this->closecol_function();
            }
            $hidden = $hidden?' style="display: none; "':null;
            if(is_null($id))
            {
                echo "<tr$hidden>";                
            }
            else
            {
                echo "<tr id=\"$id\"$hidden>";
            }
            $this->row_is_open = true;
            $this->columns_current = 0;
        }

        function end()
        {
            if($this->cell_is_open)
            {
                $this->closecol_function();
            }
            if($this->row_is_open)
            {
                $this->closerow_function();
            }
            $this->close_table_function();
        }

        function close_table_function()
        {
            echo '</tbody></table>';
        }

        private function closerow_function()
        {
            echo '</tr>';
            $this->row_is_open = false;
        }

        private function closecol_function()
        {
            echo '</td>';
            $this->cell_is_open = false;
        }

    }

?>